﻿using System;
using System.Linq;
using OS.Infrastructure.Common;
using OS.Infrastructure.Domain;
using OS.Infrastructure.Domain.UnitOfWork;
using TaskKernel.BootStrapper;
using TaskKernel.Domain;
using TaskKernel.Domain.Interface;
using TaskKernel.Domain.Services.Action;

namespace ExtractFilesToNTFS
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.Write(DateTime.Now);
            TaskKernelBootStrapper.RunConsoleApp();
            int max;
            const int few = 5;
            var fileManager = IoC.Resolve<FileManager>();
            using (var z = IoC.Resolve<IUnitOfWorkFactory>().Create())
                max = IoC.Resolve<IQueryFile>().AllFiles().Count();
            for (var i = 0; i < max; i = i + few){
                using (var unitOfWork = IoC.Resolve<IUnitOfWorkFactory>().Create()){
                    var files = IoC.Resolve<IQueryFile>().AllFiles().Skip(i).Take(few).ToList();
                    foreach (var file in files){
                        var buffer = file.Content;
                        if (file.Content == null) continue;
                        var hash = Tools.GetMD5HashCode(buffer);
                        try{
                            fileManager.SaveToFileSystem(Convert.ToString(file.Task.UID), file.Task.Code, file.FileName, Convert.ToString(hash), buffer);
                            file.SetHash(hash);
                            IoC.Resolve<ICommandRepository<File>>().Save(file);
                        }
                        catch{}
                    }
                    unitOfWork.Commit();
                }
            }
            Console.Write(DateTime.Now);
            Console.ReadKey();
        }

        //private static void LoadFromDisk(int taskId, int fileId, string fname)
        //{
        //    var path = Path.Combine(Root, taskId.ToString(CultureInfo.InvariantCulture));
        //    var filename = path + @"\" + taskId + "_" + fileId + fname;
        //    try
        //    {
        //        using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
        //        {
        //            Console.Write("OK: {0}", filename);
        //            Console.ReadKey();
        //        }
        //    }
        //    catch (Exception exception)
        //    {
        //        Console.Write(exception.Message + "\n");
        //    }
        //}
        //private static void SaveToDisk(int skip)
        //{
        //    foreach (var file in files)
        //    {
        //        if (file.Content != null)
        //        {
        //            var path = Path.Combine(Root, file.Task.UID.ToString(CultureInfo.InvariantCulture));
        //            Directory.CreateDirectory(path);
        //            var indexStartExt = file.FileName.LastIndexOf(".", StringComparison.Ordinal);
        //            var fname = indexStartExt > 0 ? file.FileName.Substring(indexStartExt) : file.FileName;
        //            var filename = path + @"\" + file.Task.UID + "_" + file.UID + fname;
        //            try
        //            {
        //                using (var stream = new FileStream(filename, FileMode.CreateNew, FileAccess.Write))
        //                {
        //                    var buffer = file.Content;
        //                    stream.Write(buffer, 0, buffer.Length);
        //                    stream.Flush();
        //                }
        //            }
        //            catch (Exception exception)
        //            {
        //                Console.Write(exception.Message + "\n");
        //                Console.Write(indexStartExt);
        //            }
        //        }
        //    }
        //}
    }
}